<?php
namespace ZfcUser\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;
use Zend\Paginator\Paginator;
class TusergroupsTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function getAllfriends(){
		if(isset($_SESSION['user']['userId'])){
			$userId=$_SESSION['user']['userId'];
		}else{
			$userId='';
		}
		$select = $this->tableGateway->getSql()->select();
		$select	->join('t_user', 't_user_groups.friend_id=t_user.user_id',array('*'),'left');
		$select	->join(array("rackboard"=>'resource_rack'), 't_user.board_id=rackboard.rack_id',array('*'),'left');
		$select	->join(array("rackboardname"=>'rack_name'), 'rackboardname.rack_name_id=rackboard.rack_name_id',array('board_name' => new Expression('rackboardname.name')),'left');
		$select	->join(array("rackclass"=>'resource_rack'), 't_user.class_id=rackclass.rack_id',array('*'),'left');
		$select	->join(array("rackclassname"=>'rack_name'), 'rackclassname.rack_name_id=rackclass.rack_name_id',array('class_name' => new Expression('rackclassname.name')),'left');
		$select->where('t_user_groups.user_id="'.$userId.'"');
		$row = $this->tableGateway->selectWith($select);			
        return $row;
	}
	public function getAllfriendsrequests(){
		if(isset($_SESSION['user']['userId'])){
			$userId=$_SESSION['user']['userId'];
		}else{
			$userId='';
		}
		$select = $this->tableGateway->getSql()->select();
		$select	->join('t_user', 't_user_groups.user_id=t_user.user_id',array('*'),'left');
		$select	->join(array("rackboard"=>'resource_rack'), 't_user.board_id=rackboard.rack_id',array('*'),'left');
		$select	->join(array("rackboardname"=>'rack_name'), 'rackboardname.rack_name_id=rackboard.rack_name_id',array('board_name' => new Expression('rackboardname.name')),'left');
		$select	->join(array("rackclass"=>'resource_rack'), 't_user.class_id=rackclass.rack_id',array('*'),'left');
		$select	->join(array("rackclassname"=>'rack_name'), 'rackclassname.rack_name_id=rackclass.rack_name_id',array('class_name' => new Expression('rackclassname.name')),'left');
		$select->where('t_user_groups.friend_id="'.$userId.'"');
		$row = $this->tableGateway->selectWith($select);		
        return $row;
	}
	public function checkEmail($friendid)
    {	
		if(isset($_SESSION['user']['userId'])){
			$userId=$_SESSION['user']['userId'];
		}else{
			$userId='';
		}
		$select = $this->tableGateway->getSql()->select()			
				->where("friend_id='".$friendid."'")
				->where("user_id='".$userId."'");					
		$resultSet = $this->tableGateway->selectWith($select);		
		$row = $resultSet->count();			
		return $row;
		
	}
	public function addfriends($friendid){
		if(isset($_SESSION['user']['userId'])){
			$userId=$_SESSION['user']['userId'];
		}else{
			$userId='';
		}
		$data = array(
			'user_id'  				=> 	$userId,
			'friend_id'  			=> 	$friendid,
			'added_date'  			=> 	date('Y-m-d H:i:s'),
			'group_status'			    =>	0, 			
		);
		$result=$this->tableGateway->insert($data);
		return $result;		
	}
	public function deleteOperation($ids){
		$this->tableGateway->delete(array('(group_id IN ('.$ids.'))'));	
		return $this->tableGateway->lastInsertValue;	
	}
	public function changeStatus($ids,$value){ 	
		$data = array(
			'group_status' =>	$value,
		);
		$row=$this->tableGateway->update($data, array('group_id' => $ids));
		return 	$row;	
	}

}